<template>
 <div>
   <p class="f16">商品管理</p>
   <div class="mt-20 flex flex-column align-center justify-center">
     <Table :columns="columns" :data="tableDate"></Table>
     <Page 
      :total="page.total"
      :current="page.pageNum"
      :page-size="page.pageSize"
      show-sizer
      @on-change="pageChange"
      @on-page-size-change="pageSizeChange"
      class="my-20"></Page>
   </div>
   <Modal
      v-model="isShow"
      :title="content"
      width="600px">
      <div v-show="orderInfo.length">
        <Form v-for="item in orderInfo" :key="item.orderNo" class="blank-border mt-10 px-10">
          <FormItem label="订单号:">
            {{ item.orderNo }}
          </FormItem>
          <FormItem label="商品名称:">
            {{ item.productName }}
          </FormItem>
          <FormItem label="商品单价:">
            {{ item.unitPrice / 100 }}元
          </FormItem>
          <FormItem label="商品数量:">
            {{ item.quantity }}个
          </FormItem>
          <FormItem label="商品总价:">
            {{ item.totalPrice / 100 }}元
          </FormItem>
        </Form>
      </div>
      <div v-show="isShowConsignee">
        <Form class="blank-border mt-10 px-10">
          <FormItem label="收件人:">
            {{ consigneeList.receiverName }}
          </FormItem>
          <FormItem label="收货地址:">
            {{ consigneeList.receiverAddress }}
          </FormItem>
          <FormItem label="电话号码:">
            {{ consigneeList.receiverMobile }}
          </FormItem>
        </Form>
      </div>
   </Modal>
 </div>
</template>
<script>
import { orderList, orderDelivered, orderFinish } from 'api'
export default {
  data () {
    return {
      isShow: false,
      content: '订单信息',
      orderInfo: [],
      consigneeList: {},
      isShowConsignee: false,
      columns: [
        {
          key: 'orderNo',
          title: '订单号',
          align: 'center'
        },
        {
          key: 'totalPrice',
          title: '订单总价',
          align: 'center'
        },
        {
          key: 'orderStatus',
          title: '订单状态',
          align: 'center',
          render: (h,params) => {
            return <p>
              {this.orderStatus.filter(item => item.type === params.row.orderStatus)[0].label}
            </p>
          }
        },
        {
          key: 'paymentType',
          title: '支付方式',
          align: 'center',
          render: (h,params) => {
            if (params.row.paymentType === 1) {
              return <p>在线支付</p>
            }
          }
        },
        {
          title: '创建时间',
          align: 'center',
          render: (h,params) => {
            return <div>
              { this.format(params.row.createTime, 'yyyy年MM月dd日hh时mm分ss秒') }
            </div>
          }
        },
        {
          title: '操作',
          align: 'center',
          width: '460px',
          render: (h,params) => {
            return <div class="flex align-center justify-between">
              <Button onClick={this.openOrderInfo.bind(this, params.row)}>查看订单信息</Button>
              <Button onClick={this.openOrderUserInfo.bind(this, params.row)}>查看收件人信息</Button>
              <Button onClick={this.deliverGoods.bind(this, params.row)} disabled={params.row.orderStatus != 20} type="success" >发货</Button>
              <Button type='error' onClick={this.orderOver.bind(this, params.row)} disabled={params.row.orderStatus != 30}>订单完结</Button>
            </div>
          }
        },
      ],
      orderStatus: [
        {
          type: 0,
          label: '用户已取消'
        },
        {
          type: 10,
          label: '未付款'
        },
        {
          type: 20,
          label: '已付款'
        },
        {
          type: 30,
          label: '已发货'
        },
        {
          type: 40,
          label: '交易完成'
        }
      ],
      tableDate: [],
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 10,
      }
    }
  },
  created() {
    this.orderList()
  },
  mounted() {

  },
  methods: {
    // list
    orderList() {
      orderList(this.page).then(res=>{
        this.tableDate = res.list
        this.page.pageNum = res.pageNum
        this.page.pageSize = res.pageSize
        this.page.total = res.total
      })
    },
    // 查看订单信息
    openOrderInfo(data) {
      this.isShow = true
      this.content = '订单信息'
      this.orderInfo = data.orderItemVOList
    },

    // 查看收件人信息
    openOrderUserInfo(data) {
      this.isShow = true
      this.content = '收件人信息'
      this.orderInfo = []
      this.consigneeList = data
      this.isShowConsignee = true
    },

    // 已发货
    deliverGoods(data) {
      this.$Modal.confirm({
        title: '发货提示',
        content: '确认该订单已发货吗？',
        onOk: () => {
          orderDelivered(data.orderNo).then(res =>{
            this.$Message.success('发货成功!')
            this.orderList()
          })
        }
      })
     
    },

    // 订单完结
    orderOver(data) {
      this.$Modal.confirm({
        title: '订单完结提示',
        content: '确认完结该订单吗？',
        onOk: () => {
          orderFinish(data.orderNo).then(res => {
            this.$Message.success('订单完结成功!')
            this.orderList()
          })
        }
      })
      
    },

    // 页码
    pageChange(pageNum) {
      this.page.pageNum = pageNum
      this.orderList()
    },
    // 页数
    pageSizeChange(pageSize) {
      this.page.pageSize = pageSize
      this.orderList()
    },

    format(date, fmt) {
      date = new Date(date)
      const o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        'S': date.getMilliseconds() // 毫秒
      }
      if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) }
      for (const k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
        }
      }
      return fmt
    }

  },
}
</script>
<style lang='less' scoped>
.blank-border{
  border-radius: 10px;
  border: 1px solid rgba(1,1,1,0.6);
}
</style>